<?php
class NewsAction extends MbaseAction {
	public $tbname = 'News';
	public $obj = '';
	public $mpk = 'news_id';
	public $cate = 'news_cate';
	public function _initialize() {
		parent :: _initialize() ;
		$this->assign('isnews',1);
	}
	public function index(){
		$cate_id = isset($_GET['cate_id'])? $_GET['cate_id']:0;
		$this->getHot($cate_id);
		$this->getSpc($cate_id);
		$this->setAdv($cate_id);
		parent :: index();
	}
	public function show() {
		$mpk_val = isset ($_GET[$this->mpk]) ? $_GET[$this->mpk] : 0;
		if (!$mpk_val)
			$this->error('无数据', 0);
		$data = $this->getMap();
		$data['par']['p'] = 0;
		$datainfo =  $this->obj->doGet($data['par']);
		if (!$datainfo) {
			$this->error($this->obj->getError(), 0);
			return false;
		} else {
			$map[$this->mpk] = $mpk_val;
			$this->obj->setInc('clicked',$map,1);
			$good_ids = $datainfo['good_ids'];
			$gmap['good_id'] = array('in',$good_ids);
			if(!empty($gmap)){
				$mgood = M('Good');
				$goodlist = $mgood->where($gmap)->select();
				//echo $mgood->getLastSql();
				$this->assign('goodlist',$goodlist);
				//dump($goodlist);
			}
			$this->assign('datainfo', $datainfo);
			$cate_id = $datainfo['cate_id'];
			$this->getHot($cate_id);
			$relalist = $this->getRela($datainfo['keywords'],$datainfo['news_id']);
			$this->getSpc($cate_id);
			$this->assign('relalist',$relalist);
			$this->setAdv($cate_id);
			$this->display();
		}
	}
	public function getHot($cate_id = 0){
		$catemap = $this->getCateMap($cate_id);
		$datalist = $this->obj->where($catemap)->order('clicked desc')->limit(10)->select();
		$this->assign('hotlist',$datalist);
	}
	public function getSpc($cate_id = 0){
		$catemap = $this->getCateMap($cate_id);
		$catemap['is_topic'] = 1;
		$catemap['thumb'] = array('neq','');
		$datalist = $this->obj->where($catemap)->order('clicked desc')->limit(2)->select();
		//dump($datalist);
		$this->assign('spclist1',$datalist);
		$catemap['thumb'] = '';
		$datalist = $this->obj->where($catemap)->order('clicked desc')->limit(10)->select();
		$this->assign('spclist2',$datalist);
	}
	
	public function getRela($keywords = '',$news_id = 0){
		$keywords = trim($keywords);
		if(empty($keywords)) return null;
		$map['tag'] = array('in',$keywords);
		$mtag = M('Tag');
		$mnt = M('News_tag');
		$tag_ids = $mtag->where($map)->field('tag_id')->select();
		$tids = array();
		foreach($tag_ids as $vo){
			$tids[] = $vo['tag_id'];
		}
		$ntmap['tag_id'] = array('in',implode(',',$tids));
		if($news_id) $ntmap['news_id'] = array('neq',$news_id);
		$news_ids = $mnt->where($ntmap)->group('news_id')->field('news_id')->limit(10)->select();
		$tnids = array();
		foreach($news_ids as $vo){
			$tnids[] = $vo['news_id'];
		}
		$nmap['news_id'] = array('in',implode(',',$tnids));
		$datalist = $this->obj->where($nmap)->select();
		return $datalist;
	}
	
	public function setAdv($cate_id = 0){
		$mcate = M('News_cate');
		$map = array();
		if($cate_id) $map['cate_id'] = $cate_id;
		$datainfo = $mcate->where($map)->find();
		$this->assign('adv1',$datainfo['adv1']);
		$this->assign('adv2',$datainfo['adv2']);
	}
}
?>